/**
 * 数据整合
 * 
 * @param {any} arr1 已选择数据组合
 * @param {any} arr2 existSku 数据组合
 */
function groupArray(arr1, arr2) {
    var _arr1 = arr1;
    var _arr2 = arr2;

    if(!_arr1) return;


    if(typeof _arr1 === "string") _arr1 = JSON.parse(_arr1);
    if(typeof _arr2 === "string") _arr2 = JSON.parse(_arr2);

    if(_arr2.length === 0) return _arr1;

    for(i in _arr1) {
        for(k in _arr2) {
            if(_arr1[i].id == _arr2[k].sku_id ) {
                _arr1[i].exist = _arr2[k].exist;
                _arr1[i].limit_point = _arr2[k].limit_point;
                _arr1[i].limit_number = _arr2[k].limit_number;
                _arr1[i].active_price = _arr2[k].active_price;
            }
        }
    }
    return _arr1;
}


/**
 * 
 * 数据去重
 * @param {Array} oldArr 已存在数据值
 * @param {Array} newArr 新选择数据值
 * @returns {Array} 去重后需要新增数据
 */
function findAgain(oldArr, newArr) {
    var listId = getListId(); //已有数据ID
    var dom = $('#selectTable tbody tr');
    var skuIdAttr = []; //已有id

    var newId = []; //新数据 ID
    for(i in newArr) {
        newId.push(newArr[i].id);
    }

    for(var i = oldArr.length-1; i >= 0 ; i--) {
        var _id = $.inArray(oldArr[i].id, newId);
        if(_id === -1) {
            // $('#selectTable tbody tr').eq(_id).remove();
        }
        else {
            newArr.splice(_id, 1);
        }
    }

    for(var i = newArr.length-1; i >= 0 ; i--) {
        var _id = $.inArray(newArr[i].id, listId);

        if(_id === -1) {
            oldArr.push(newArr[i]);
        }
        else {
            oldArr.splice(_id, 1);
            $('#selectTable tbody tr').eq(_id).remove();

        }
    }

    return (newArr);
}
//获取已选列表
function getListId() {
    var dom = $('#selectTable tbody tr');
    var arr = [];
    
    for( i in dom) {
        if($('#selectTable tbody tr').eq(i).find('input[data-skuid]').val()) {
            arr.push(+$('#selectTable tbody tr').eq(i).find('input[data-skuid]').val());
        }
    }
    return arr;
}

//商品删除
$(document).on('click', '.js-remove', function() {
    var index = $(this).parents('tr').index();

    var selectTab = JSON.parse($('#select-label input[name=goods_tab]').val());

    selectTab.splice(index, 1);
    $('#selectTable tbody tr').eq(index).remove();

    $('#select-label input[name=goods_tab]').val(JSON.stringify(selectTab));

});

//添加商品
var addTableHtml = function(item, type) {
    var stateType = /restriction\/add/gi.test(window.location.href) ? 1 : 2; //判断是新增还是编辑

    var data = stateType===1 ?  item : groupArray(item, existSku);

    var trLen = $('#selectTable tbody tr').length;
    if(data.length === 0) return;
    for (var i = 0, len = data.length; i < len; i++) {
 
        var id = data[i].id ? data[i].id : "";
        var goods = data[i].goods ? data[i].goods : "";
        var exist = data[i].exist ? data[i].exist : 0;
        var name = data[i].name ? data[i].name : "";
        var sku = data[i].sku ? data[i].sku : "";
        var attr = data[i].attr ? data[i].attr : "";
        var limit_number = data[i].limit_number ? data[i].limit_number : "";
        var active_price = data[i].active_price ? data[i].active_price : "";
        var limit_point = data[i].limit_point ? data[i].limit_point : "";
        
        var deleteHtmlStr = ""; //删除按钮
        var existHtmlStr = ""; //exist 标签
        
        if(type === 1) {
            deleteHtmlStr = '<td width="10%"><a href="javascript:;" class="js-remove">删除</a></td>';
        }
        else if(type === 2) {
            existHtmlStr = '<input type="hidden" name="exist[]" value=' + exist + '>';
        }
        var html = 	
                '<tr>'+
                existHtmlStr +
                '	<input type="hidden" name="goods_id[]" value=' + goods + '>'+
                '	<input type="hidden" data-skuid=' + id + ' name="sku_id[]" value=' + id + '>'+
                '	<td width="5%"><input type="checkbox" name=""></td>'+
                '	<td width="20%" title=' + name + '>' + name + '</td>'+
                '	<td width="20%">' + sku + '</td>'+
                '	<td width="10%">' + attr + '</td>'+
                '	<td width="10%"><input type="text" name="limit_point[]" class="form-control" value='+ limit_point +'></td>'+
                '	<td width="10%"><input type="text" name="limit_number[]" class="form-control js-attr-number" value='+ limit_number +'></td>'+
                '	<td width="10%"><input type="text" name="active_price[]" class="form-control js-attr-price" value='+ active_price +'></td>'+
                deleteHtmlStr +
                '</tr>';

        if(trLen !== 0) {
            $('#selectTable tbody tr:last-child').after(html);
        }
        else {
            $('#selectTable tbody').append(html);
        }
    }

}

//全选
$('#jsAllSelect').click(function() {
    var b_state = $(this).find('input[type=checkbox]').is(':checked');

    if(b_state) {
        $(this).find('input[type=checkbox]').attr('checked', false);
        $(this).parents('#selectTable').find('tbody td input[type=checkbox]').attr('checked', false);
    }
    else {
        $(this).find('input[type=checkbox]').attr('checked', true);
        $(this).parents('#selectTable').find('tbody td input[type=checkbox]').attr('checked', true);
    }
});

//批量删除
function allRemove() {
    var tbody = $('#selectTable tbody tr');
    var stateArr = []; //存储是否勾选状态
    var selectTab = JSON.parse($('#select-label input[name=goods_tab]').val());

    for(var i=0, len = tbody.length; i < len; i++) {
        var b_state = $('#selectTable tbody tr').eq(i).find('td input[type=checkbox]').attr('checked');
        stateArr.push(b_state ? 1 : 0);
    }

    for(var i = stateArr.length-1; i >= 0 ; i--) {
        if(stateArr[i]) {
            selectTab.splice(i, 1);
            $('#selectTable tbody tr').eq(i).remove();
        }
    }

    $('#select-label input[name=goods_tab]').val(JSON.stringify(selectTab));
}

//表格失去焦点存储数据
$(document).on('blur', '#selectTable tbody input[type=text]', function() {
    var id = +$(this).parents('tr').find('input[data-skuid]').val();
    var name = $(this).attr('name');
    var data = $(this).val();

    saveAttr(id, name, data);
}) 

//保存参数
function saveAttr(id, name, data) {
    var selectTab = JSON.parse($('#select-label input[name=goods_tab]').val());
    
    for(i in selectTab) {
        if(selectTab[i].id === id) {
            if(name === 'limit_point[]') {
                selectTab[i].limit_point = data;
            }
            else if(name === 'limit_number[]') {
                selectTab[i].limit_number = data;
            }
            else if(name === 'active_price[]') {
                selectTab[i].active_price = data;
            }

        }
    }

    $('#select-label input[name=goods_tab]').val(JSON.stringify(selectTab));

}

//批量操作
$('#limitNumber').click(function(){
    allCopy(1, this);
});
//批量操作
$('#limitPrice').click(function(){
    allCopy(2, this);
});

//批量操作设置参数
function allCopy(type, me) {
    var self = me;
    var selectTab = JSON.parse($('#select-label input[name=goods_tab]').val());//数据
    var number = $(self).siblings('div').find('input[type=number]').val(); //批量参数
    
    var stateType = /restriction\/add/gi.test(window.location.href) ? 1 : 2; //判断是新增还是编辑

    if(!number) return;

    for(i in selectTab) {
        if(type === 1) {
            if(stateType === 2) {
                for(k in groupSkuArray) {
                    if(selectTab[i].id == groupSkuArray[k].id) {
                        selectTab[i].limit_number = groupSkuArray[k].limit_number ? groupSkuArray[k].limit_number : number;
                        $('#selectTable tbody tr').eq(i).find('input.js-attr-number').val(selectTab[i].limit_number);
                    }
                }
            }
            else {
                selectTab[i].limit_number =selectTab[i].limit_number ?selectTab[i].limit_number : number;
                $('#selectTable tbody tr').eq(i).find('input.js-attr-number').val(selectTab[i].limit_number);
            }
            
        }
        else if(type === 2) {
            if(stateType === 2) {
                for(k in groupSkuArray) {
                    if(selectTab[i].id == groupSkuArray[k].id) {
                        selectTab[i].active_price = groupSkuArray[k].active_price ? groupSkuArray[k].active_price : number;
                        $('#selectTable tbody tr').eq(i).find('input.js-attr-price').val(selectTab[i].active_price);
                    }
                }
            }
            else {
                selectTab[i].active_price =selectTab[i].active_price ?selectTab[i].active_price : number;
                $('#selectTable tbody tr').eq(i).find('input.js-attr-price').val(selectTab[i].active_price);

            }

        }
    }

    $('#select-label input[name=goods_tab]').val(JSON.stringify(selectTab));
}

